/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';

    angular
        .module('com.akxx.aqzyk')
        .controller('CourseController', CourseController);


    /** @ngInject */
    function CourseController($timeout, $state, SweetAlert, _Course) {
        var vm = this;
        vm.newFrame = false;
        vm.newNav = false;
        vm.overlay = false;
        vm.courseItem = {};
        vm.systemItem = {};
        vm.isReadonly = false;
        getLeftbarList({async: true, pageSize: 0, pageIndex: 25, sortExpr: "name desc"});
        function getLeftbarList(param) {
            _Course.SystemList.get(param).then(function (res) {
                vm.naviBInit = res.data;
                vm.active = vm.naviBInit[0].id;
                var searchCode = vm.naviBInit[0].code;
                getCourseInfo({async: true,sortExpr:"name desc","q[fld op]":searchCode});
            });
        };
        function  getCourseInfo(){
            _Course.CourseList.get(param).then(function (res) {
                vm.course = res.data;
            });
        }
        //关闭弹窗
        vm.closeFrame = function(){
            vm.newNav = false;
            vm.newFrame = false;
            vm.overlay = false;
            vm.isReadonly = false;
        };
        //左侧hover事件
        vm.leftItemShow = function (id) {
            vm.isShow = id;
        }
        vm.leftItemHide = function (id) {
            vm.isShow = "";
        }
        //新建左侧导航
        vm.addLeftNav = function () {
            vm.newNav = true;
            vm.overlay = true;
            vm.saveNavType = "new";
        }
        //编辑左侧导航
        vm.editLeftNav = function(id){
            var param={id:id}
            _Course.EditSystem.get(param).then(function (res) {
                if(res.success){
                    vm.newSystem = res.data;
                    vm.newNav = true;
                    vm.overlay = true;
                    vm.saveNavType = "edit"
                    vm.saveNavId = id;
                }
            });

        };
        // 保存
        vm.saveLeftNav = function(){
            if(vm.saveType = "new"){
                var param = vm.systemItem;
                _Course.NewClass.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.closeFrame();
                    }
                });
            }else if(vm.saveType = "edit"){
                var param = vm.systemItem;
                param.id = vm.saveNavId;
                _Course.AddSystem.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("编辑成功");
                        vm.closeFrame();
                    }
                });
            }
            getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }
        // 删除左侧导航
        vm.delLeftNav = function(id){
            var param = {id:id}
            _Course.DelSystem.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功");
                    getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
                }
            });
        }

        //保存
        vm.saveNewStu = function () {
            if(vm.saveType = "new"){
                var param = vm.newStu;
                _Student.NewStudent.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功")
                        vm.closeFrame();
                    }
                });
            }else if(vm.saveType = "edit"){
                var param = vm.stu;
                param.id = vm.saveStuID;
                _Student.EditStudent.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.closeFrame();
                    }
                });
            }
            getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        vm.leftBarClick = function (id) {
            vm.active = id;
            for (var i = 0; i < vm.naviBInit.length; i++) {
                if (vm.naviBInit[i].id == id) {
                    var searchCode = vm.naviBInit[i].code;
                    getCourseInfo({async: true,sortExpr:"name desc","q[fld op]":searchCode});
                }
            }
        }



        // 右侧hover事件
        vm.itemShow = function (id) {
            vm.isShow = id;
        }
        vm.itemHide = function (id) {
            vm.isShow = "";
        }
        vm.itemAdd = function (id) {
            vm.newFrame = true;
            vm.overlay = true;
            vm.saveCouType = "new";
        }
        vm.itemEdit = function (id) {
            _Course.VisitCourse.get(param).then(function (res) {
                if(res.success){
                    vm.courseItem = res.data;
                    vm.newFrame = true;
                    vm.overlay = true;
                    vm.saveCouType = "edit";
                    vm.saveCourseId = id;
                }
            });
        }
        //保存
        vm.saveNewCourse = function () {
            if(vm.saveCouType = "new"){
                var param = vm.courseItem;
                _Course.AddCourse.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功")
                        vm.closeFrame();
                    }
                });
            }else if(vm.saveCouType = "edit"){
                var param = vm.courseItem;
                param.id = vm.saveCourseId;
                _Course.EditCourse.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.closeFrame();
                    }
                });
            }
            getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        vm.itemVisit = function(id){
            var param={id:id}
            _Course.VisitCourse.get(param).then(function (res) {
                if(res.success){
                    vm.courseItem = res.data;
                    vm.isReadonly = true;
                    vm.newFrame = true;
                    vm.overlay = true;
                }
            });
        }

        vm.itemDel = function (id) {
            var param = {id: id}
            _Course.DelCourse.get(param).then(function (res) {
                if (res.success) {
                    SweetAlert.swal("删除成功！");
                    getLeftbarList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
                } else {
                    SweetAlert.swal("删除失败！");
                }
            });
        }
    }

})();

